import { reactive } from "vue";
import { U16, U32, U8 } from "./codec";
import { Message } from "./message";
import { hex4 } from "./ui";

export interface WeightSwSensor {
    front_check: boolean,
    back_check: boolean,
    front_fault: boolean,
    back_fault: boolean,
}

export interface WeightState {

    version: string,
    sensor: WeightSwSensor,
    front: number,
    back: number,
}

export const weightState = reactive<WeightState>({
    version: '',
    sensor: {
        front_check: false,
        back_check: false,
        front_fault: false,
        back_fault: false,
    },
    front: 0,
    back: 0,
});

export function onWeightStatus(msg: Message) {
    const version = new U16();
    const sensor = new U8();
    const front = new U32();
    const back = new U32();

    msg.parseResult(version, sensor, front, back);

    weightState.version = hex4(version.value);
    weightState.sensor = {
        front_check: sensor.isBitSet(0),
        back_check: sensor.isBitSet(1),
        front_fault: sensor.isBitSet(2),
        back_fault: sensor.isBitSet(3),
    };
    weightState.front = front.value;
    weightState.back = back.value;
}

